﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using FS.Business;
using FS.Common.DotNetCode;
using FS.Common.DotNetUI;

namespace FS.Web.BLL.Visa_info
{
    public partial class Visa_InfoPostList : PageBase
    {       
        public string _key;
        private visa_dao _visa = new visa_dao();
        protected void Page_Load(object sender, EventArgs e)
        {
            this._key = base.Request["key"];
            this.PageControl1.pageHandler += new EventHandler(this.pager_PageChanged);
            if (!base.IsPostBack)
            {
                GetSelect_TrustInfo();
                GetSelect_City();
                GetSelect_Certtype();
                this.begdate.Value = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd");
                this.enddate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }

        protected void pager_PageChanged(object sender, EventArgs e)
        {
            this.DataBindGrid();
        }

        #region 初始项目数据绑定
        //委托方
        private void GetSelect_TrustInfo()
        {
            DataTable dt = _visa.GetSelect_TrustInfo();
            ControlBindHelper.BindHtmlSelect(dt, this.sel_trust, "name", "trust_id", "请选择");
        }

        //所属国家
        private void GetSelect_City()
        {
            DataTable dt = _visa.GetSelect_CityInfo();
            ControlBindHelper.BindHtmlSelect(dt, this.sel_cityid, "city_name", "city_id", "请选择");
        }

        //签证类型
        private void GetSelect_Certtype()
        {
            DataTable dt = _visa.GetSelect_CertType();
            ControlBindHelper.BindHtmlSelect(dt, this.sel_certtype, "property_value", "property_id", "请选择");
        }
        #endregion

        private void DataBindGrid()
        {
            if (this.Hchange.Value == "1")
            {
                this.PageControl1.PageIndex = 1;
                this.Hchange.Value = "0";
            }
            BackScript("query");
        }

        protected void lbtSearch_Click(object sender, EventArgs e)
        {
            this.DataBindGrid();
        }

        protected void Download_Click(object sender, EventArgs e)
        {
            BackScript("export");
        }

        private void BackScript(string scripttype)
        {
            StringBuilder SqlWhere = new StringBuilder();
            IList<SqlParam> IList_param = new List<SqlParam>();

            if (!string.IsNullOrEmpty(this.begdate.Value))
            {
                SqlWhere.Append(" and detail.InfoDetail_Receivedate >= @begdate ");
                IList_param.Add(new SqlParam("@begdate", this.begdate.Value.Trim()));
            }
            if (!string.IsNullOrEmpty(this.enddate.Value))
            {
                SqlWhere.Append(" and detail.InfoDetail_Receivedate <= @enddate ");
                IList_param.Add(new SqlParam("@enddate", this.enddate.Value.Trim()));
            }
            
            if (!string.IsNullOrEmpty(this.sel_cityid.Value))
            {
                SqlWhere.Append(" and detail.City_id = @City_id ");
                IList_param.Add(new SqlParam("@City_id", this.sel_cityid.Value.Trim()));
            }
            if (!string.IsNullOrEmpty(this.sel_certtype.Value))
            {
                SqlWhere.Append(" and detail.ref_certtype = @ref_certtype ");
                IList_param.Add(new SqlParam("@ref_certtype", this.sel_certtype.Value.Trim()));
            }
            if (!string.IsNullOrEmpty(this.cusname.Value))
            {
                SqlWhere.Append(" and detail.InfoDetail_user = @InfoDetail_user ");
                IList_param.Add(new SqlParam("@InfoDetail_user", this.cusname.Value.Trim()));
            }
            if (!string.IsNullOrEmpty(this.cusphone.Value))
            {
                SqlWhere.Append(" and detail.InfoDetail_userphone = @obj ");
                IList_param.Add(new SqlParam("@obj", this.cusphone.Value.Trim()));
            }
            if (!string.IsNullOrEmpty(this.cusno.Value))
            {
                SqlWhere.Append(" and detail.InfoDetail_cardno = @InfoDetail_cardno ");
                IList_param.Add(new SqlParam("@InfoDetail_cardno", this.cusno.Value.Trim()));
            }
            if (!string.IsNullOrEmpty(this.sel_trust.Value))
            {
                SqlWhere.Append(" and info.trust_id = @trust_id ");
                IList_param.Add(new SqlParam("@trust_id", this.sel_trust.Value.Trim()));
            }

            switch (scripttype)
            {
                case "query":
                    int count = 0;
                    DataTable dt = this._visa.GetInfoPostList(SqlWhere, IList_param, this.PageControl1.PageIndex, this.PageControl1.PageSize, ref count);
                    ControlBindHelper.BindRepeaterList(dt, this.rp_Item);
                    this.PageControl1.SetPageCount(count);
                    break;
                case "export":
                    //DataTable dt2 = this._visa.GetInfoShipList(SqlWhere, IList_param);
                    //ExcelHelper.ExportExcel(dt2, "送签" + DateTime.Now.ToString("yyyy-MM-dd"));
                    break;
                default:
                    break;
            }

        }
    }
}